package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.CommunityHouseTypeDTO;
import com.daxia.bumall.model.CommunityHouseType;
import com.daxia.bumall.model.HouseType;
import com.daxia.bumall.support.Page;

@Repository
public class CommunityHouseTypeDAO extends GenericDAOHibernate<CommunityHouseType> {

    public List<CommunityHouseType> find(CommunityHouseTypeDTO dto, Page page) {
        StringBuilder hql = new StringBuilder(); 
        List<Object> paras = new ArrayList<Object>();
        hql.append("select distinct n From CommunityHouseType n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getCommunity() != null) {
        	if (dto.getCommunity().getId() != null) {
	            hql.append(" and n.community.id = ? ");
	            paras.add(dto.getCommunity().getId());
        	}
        	if(dto.getCommunity().getCity() != null && dto.getCommunity().getCity().getId() != null) {
        		hql.append(" and n.community.city.id = ? ");
        		paras.add(dto.getCommunity().getCity().getId());
        	}
        	if(dto.getCommunity().getDistrict() != null && dto.getCommunity().getDistrict().getId() != null) {
        		hql.append(" and n.community.district.id = ? ");
        		paras.add(dto.getCommunity().getDistrict().getId());
        	}
        }	
        if(dto.getHouseStyle() != null && dto.getHouseStyle().getId() != null) {
    		hql.append(" and d.houseStyle.id = ? ");
    		paras.add(dto.getHouseStyle().getId());
    	}
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public List<CommunityHouseType> find2(CommunityHouseTypeDTO dto, Page page) {
        StringBuilder hql = new StringBuilder(); 
        List<Object> paras = new ArrayList<Object>();
        hql.append("select distinct n From CommunityHouseType n,Design d where n.id=d.communityHouseType.id ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getCommunity() != null) {
        	if (dto.getCommunity().getId() != null) {
	            hql.append(" and n.community.id = ? ");
	            paras.add(dto.getCommunity().getId());
        	}
        	if(dto.getCommunity().getCity() != null && dto.getCommunity().getCity().getId() != null) {
        		hql.append(" and n.community.city.id = ? ");
        		paras.add(dto.getCommunity().getCity().getId());
        	}
        	if(dto.getCommunity().getDistrict() != null && dto.getCommunity().getDistrict().getId() != null) {
        		hql.append(" and n.community.district.id = ? ");
        		paras.add(dto.getCommunity().getDistrict().getId());
        	}
        }	
        if(dto.getHouseStyle() != null && dto.getHouseStyle().getId() != null) {
    		hql.append(" and d.houseStyle.id = ? ");
    		paras.add(dto.getHouseStyle().getId());
    	}
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public CommunityHouseType findOne(CommunityHouseTypeDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<CommunityHouseType> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public List<CommunityHouseType> findHouseTypes(Long communityId) {
		String hql = "select distinct n From CommunityHouseType n where n.community.id = ?";
		return super.find(hql, new Object[] {communityId}, null);
	}
}
